'''
课后作业
通过`datetime`模块完成时间戳, `datetime.datetime`对象, 格式化字符串三者之间的转换
封装一个函数`get_date(day_delta)`, 如果传入的是`-1` , 输出就是**字符串**日期`2020-01-02`.
'''
import datetime
from datetime import timedelta
now = datetime.datetime.now()
# 时间格式转换为时间戳
time_stamp = now.timestamp()
print(time_stamp)
# 时间格式转换为字符串格式
print(now.time().strftime("%Y:%m:%d %H:%M:%S"))
# 字符串格式转换为时间格式
str_time = '2021:01:01 10:44:37'
print(datetime.datetime.strptime(str_time,"%Y:%m:%d %H:%M:%S"))
# 字符串转换为时间戳格式
print(datetime.datetime.strptime(str_time,"%Y:%m:%d %H:%M:%S").timestamp())
# 时间戳转换为时间格式
print((datetime.datetime.fromtimestamp(now.timestamp(),tz=None)).strftime("%Y:%m:%d %H:%M:%S"))
# 时间戳转换为字符串格式
# print((datetime.datetime.now(tz=None).timestamp())

def get_date():
    '''
    封装一个函数`get_date(day_delta)`, 如果传入的是`-1` , 输出就是字符串日期`2020-01-02`.
    :param day_delta:
    :return:
    '''
    n = input("想知道时间？请输入-1: ")
    if n == '-1':
        print(datetime.datetime.now(tz=None).strftime("%Y:%m:%d"))
    else:
        print("输入的不是-1，重新输入")
        get_date()
    return
get_date( )


'''
import time
import datetime
print(time.time())
# print(time.time().strftime("%Y=%m=%d %H:%M:%S"))
print("=============")
print(datetime.datetime.now())
now = datetime.datetime.now(tz=None)
print(type(now))
print(now.strftime("%Y=%m=%d %H:%M:%S"))
print(datetime.datetime.today())
print("======时间格式转换=======")
print(datetime.datetime.strptime(now.strftime("%Y-%m-%d %H:%M:%S"), "%Y-%m-%d %H:%M:%S"))
'''
'''
>>> now = datetime.datetime.now(tz=None)
>>> now
datetime.datetime(2021, 1, 6, 23, 43, 22, 981322)
>>> print(datetime.datetime.strptime(now.strftime("%Y=%m=%d %H:%M:%S"), "%Y=%m=%d %H:%M:%S"))
2021-01-06 23:43:22
>>> now = now.strftime("%Y-%m-%d %H:%M:%S")
>>> now
'2021-01-06 23:43:22'
>>> datetime.datetime.strptime(now, "%Y-%m-%d %H:%M:%S")
datetime.datetime(2021, 1, 6, 23, 43, 22)

'''
# 时间戳转换
'''
>>> now = datetime.datetime.now(tz=None)
>>> now
datetime.datetime(2021, 1, 6, 23, 52, 26, 803688)
>>> now.timestamp()
1609948346.803688
>>> ts = now.timestamp()
>>> ts
1609948346.803688
>>> type(ts)
<class 'float'>
>>> datetime.datetime.fromtimestamp(ts, tz=None)
datetime.datetime(2021, 1, 6, 23, 52, 26, 803688)
'''
# 时间运算
'''
>>> from datetime import timedelta
>>> now + timedelta(hours=-1)
datetime.datetime(2021, 1, 6, 22, 52, 26, 803688)
'''